<?php

namespace App\Admin\Actions\BuyWill;

use App\Library\Logic\OperateLogLogic;
use App\Models\BuyWillModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;

class BuyWillTransStatusAction extends RowAction
{
    public function __construct(int $status = 0)
    {
        parent::__construct();
        $this->name = $status === 0 ? '上架' : '下架';
    }

    public function handle(BuyWillModel $model): Response
    {
        if (is_null($model)) return $this->response()->error(__('errorCode.433'))->refresh();
        $status = $model->status ? 0 : 1;
        $model->status = $status;
        if ($status) $model->shelf_at = now();
        else $model->unshelf_at = now();
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.buyWill.transStatus', ['status' => $status]);
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * dialog
     * @return void
     */
    public function dialog(): void
    {
        $this->confirm('确定' . $this->name . '吗?');
    }
}
